package org.example.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.example.LoginRequestPacket;
import org.example.LoginResponsePacket;
import org.example.im.SessionUtil;

import java.util.Date;
import java.util.UUID;

public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) {
        System.out.println("客户端收到服务端的登录响应：" +loginResponsePacket);
        if (loginResponsePacket.getSuccess()) {
            System.out.println(new Date() + ": 客户端登录成功。分配的用户id为:" + loginResponsePacket.getUserId());
            System.out.println("客户端处理器也需要绑定用户、channel的关系，用于客户端进行已经登录的判定逻辑");
            SessionUtil.bindChannel(loginResponsePacket.getUserId(), ctx.channel());
        } else {
            System.out.println(new Date() + ": 客户端登录失败，原因：" + loginResponsePacket.getReason());
        }
    }
}
